/**
 * 
 */
package ar.edu.unq.svnpermission.domain;

import java.util.ArrayList;
import java.util.List;

import org.uqbar.commons.model.Entity;

/**
 * @author nicolas
 *
 */
public class Permission extends Entity{
	
	private static final long serialVersionUID = 1L;
	private Membership who;
	private Folder folder;
	private List<Access> acces = new ArrayList<Access>();
	
	public Permission()
	{
	}
	
	public Permission(Membership who, Folder folder, List<Access> access)
	{
		this.setWho(who);
		this.setFolder(folder);
		this.setAccess(access);
	}
	
	
	/**
	 * Agrega un nuevo acceso 
	 * @param acces
	 * @author nicolas
	 */
	public void addAccess(Access acces)
	{
		this.getAccess().add(acces);
	}
	
	/**
	 * Remueve un acceso 
	 * @param acces
	 * @author nicolas
	 */
	public void removeAccess(Access access)
	{
		this.getAccess().remove(access);
	}
	
	/**
	 * Remueve todos los accesos del permiso
	 * @author nicolas
	 */
	public void removeAllAccess()
	{
		this.setAccess(null);
	}
	
	//Getters and Setters
	public Membership getWho() {
		return who;
	}
	public void setWho(Membership who) {
		this.who = who;
	}
	public Folder getFolder() {
		return folder;
	}
	public void setFolder(Folder folder) {
		this.folder = folder;
	}
	public List<Access> getAccess() {
		return acces;
	}
	public void setAccess(List<Access> acces) {
		this.acces = acces;
	}
	
	public Boolean getHasReadAccess()
	{
		return (this.getAccess()==null ? false : this.getAccess().contains(new ReadAcces()));
		
	}
	
	public void setHasReadAccess(Boolean mustRead)
	{
		//Si el flag es verdadero, le asigno permisos
		if(mustRead)
		{
			if(!this.getHasReadAccess())
				this.getAccess().add(new ReadAcces());
		}
		else
		{
			this.getAccess().remove(new ReadAcces());
		}
	}	
	
	public Boolean getHasWriteAccess()
	{
		return (this.getAccess()==null ? false : this.getAccess().contains(new WriteAcces()));
	}	

	public void setHasWriteAccess(Boolean mustWrite)
	{
		//Si el flag es verdadero, le asigno permisos
		if(mustWrite)
		{
			if(!this.getHasWriteAccess())
				this.getAccess().add(new WriteAcces());
		}
		else
		{
			this.getAccess().remove(new WriteAcces());
		}
	}	

	
}
